package pacoteDao;

import java.sql.*;
import java.util.*;
import projetofinal.ConnectionFactory;
import projetofinal.Responsavel;

public class ResponsavelDAO {

    private ConnectionFactory conexao = new ConnectionFactory();
    private Connection connection;
    String query;

    public ResponsavelDAO() throws SQLException {
        if (connection == null || connection.isClosed()) {
            this.connection = conexao.getConnection();
        }
    }

    public boolean insert(Responsavel responsavel) throws SQLException {

        query = "insert into Responsavel(id,nome,cpf,rg,ddd,telefone,idaluno) values (" + responsavel.getId() + ",'" + responsavel.getNome() + "','" + responsavel.getCpf() + "','" + responsavel.getRg() + "','" + responsavel.getDdd() + "','" + responsavel.getTelefone() + "'," + responsavel.getIdaluno() + ")";
        Integer linha = conexao.executarNoQuery(query);

        conexao.close();
        return linha > 0;

    }

    public boolean update(Responsavel responsavel) throws SQLException {

        query = "update Responsavel set nome = '" + responsavel.getNome() + "', cpf = '" + responsavel.getCpf() + "', rg = '" + responsavel.getRg() + "', ddd = '" + responsavel.getDdd() + "', telefone = '" + responsavel.getTelefone() + "', idaluno = " + responsavel.getIdaluno() + " where id = " + responsavel.getId();
        Integer linha = conexao.executarNoQuery(query);

        conexao.close();
        return linha > 0;

    }

    public boolean delete(Responsavel responsavel) throws SQLException {

        query = "delete from Responsavel where id = " + responsavel.getId();
        int linhas = conexao.executarNoQuery(query);

        conexao.close();
        return linhas > 0;

    }

    public List<Responsavel> consultar() throws SQLException {

        query = "select * from Responsavel ";
        ResultSet rset = conexao.executar(query);

        List<Responsavel> responsavels = null;
        while (rset.next()) {
            responsavels = new ArrayList<Responsavel>();
            Responsavel responsavel = new Responsavel();
            responsavel.setId(rset.getInt(1));
            responsavel.setNome(rset.getString(2));
            responsavel.setCpf(rset.getString(3));
            responsavel.setRg(rset.getString(4));
            responsavel.setDdd(rset.getString(5));
            responsavel.setTelefone(rset.getString(6));
            responsavel.setIdaluno(rset.getInt(7));
            responsavels.add(responsavel);
        }

        conexao.close();
        return responsavels;

    }

    public Responsavel getById(Integer id) throws SQLException {

        query = "select * from Responsavel where id = " + id;
        ResultSet rs = conexao.executar(query);

        Responsavel responsavel = null;
        if (rs.next()) {
            responsavel = new Responsavel();
            responsavel.setId(rs.getInt(1));
            responsavel.setNome(rs.getString(2));
            responsavel.setCpf(rs.getString(3));
            responsavel.setRg(rs.getString(4));
            responsavel.setDdd(rs.getString(5));
            responsavel.setTelefone(rs.getString(6));
            responsavel.setIdaluno(rs.getInt(7));
        }

        conexao.close();
        return responsavel;

    }
}
